package com.example.loginproject.view.views

import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ValueAnimator
import android.app.LocaleConfig.STATUS_SUCCESS
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import androidx.core.animation.addListener
import com.example.loginproject.R
import com.example.loginproject.tool.dp2px
import com.example.loginproject.tool.getMode
import com.example.loginproject.tool.getSize
import kotlinx.coroutines.NonCancellable.start

class LoginAnimationView(context: Context,attrs:AttributeSet?):View(context, attrs) {
    private val mDefaultWidth = context.dp2px(200)
    private  val mDefaultHeight = context.dp2px(60)

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        //计算宽度
        var mWidth = 0
        when(getMode(widthMeasureSpec)){
            MeasureSpec.EXACTLY->mWidth = getSize(widthMeasureSpec)
            else -> mWidth = mDefaultWidth
        }
            //计算高度
        var mHeight = 0
        when(getMode(heightMeasureSpec)){
            MeasureSpec.EXACTLY->mWidth = getSize(heightMeasureSpec)
            else -> mHeight= mDefaultHeight
        }
        //告诉外部我具体尺寸
        setMeasuredDimension(mWidth,mHeight)
    }

    //尺寸改变的时候都会被调用
    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        //确定圆角半径
        mRadius = (Math.min(width,height)/2).toFloat()
        //确定最大移动距离
        mDistance = Math.max(width,height)-mRadius
    }
    private val STATUS_SUCCESS =0
    private val STATUS_FAILURE =1
    private val STATUS_LOADING =2
    private var longinStatus = STATUS_SUCCESS
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        //绘制矩形背景
         drawRoundRect(canvas)
        if(longinStatus == STATUS_SUCCESS){
            mRoundRectPaint.color = Color.parseColor("#00BAAD")
        }

//        if(longinStatus == STATUS_SUCCESS){
//            drawPicture(canvas)
//        }
    }
    private fun drawPicture(canvas: Canvas?){
        //Canvas扮演的是人的角色
        //Bitmap才是真正的画布
        //获取需要绘制的图片-》Bitmap
        val okBitmap = BitmapFactory.decodeResource(context.resources,R.drawable.gougoupcf)
        val bx = (width - okBitmap.width).toFloat()/2
        val by = (height - okBitmap.height).toFloat()/2
       //画笔
        val picPaint = Paint()
        //绘制x是left，y是top，图像必须是png，不能是xml，总之不要用系统的
        canvas?.drawBitmap(okBitmap,bx,by,picPaint)
    }
    private val mRoundRectPaint:Paint by lazy{
        Paint().apply{
            isAntiAlias = true//AC 33 C1
            color = Color.rgb(172,51,241)
            style = Paint.Style.FILL
        }
    }
    private var mRadius:Float = 0f
    private var mCurrentRadius:Float = 0f
    private var mDistance:Float = 0f//从两边移动到中心圆的最大距离
    private var mCurrentDistance:Float = 0f//动画时的距离
    private fun drawRoundRect(canvas:Canvas?){
    //确定矩形区域
    // 确定圆角半径
    canvas?.drawRoundRect(mCurrentDistance,0f,width.toFloat()-mCurrentDistance,height.toFloat(),mCurrentRadius,mCurrentRadius,mRoundRectPaint)
}
    fun startAnim(){
        //ValueAnimater从0变成mRadius
      val radiusAnim = ValueAnimator.ofFloat(0f, mRadius).apply{
            duration = 500
            addUpdateListener {
                mCurrentRadius = it.animatedValue as Float
                invalidate()
            }
      }
        //两边向中心靠拢
        val moveAnim= ValueAnimator.ofFloat(0f, mDistance).apply{
            duration =700
            addUpdateListener {
                mCurrentDistance = it.animatedValue as Float
                invalidate()
            }
        }
        //使用AnimatorSet管理所有动画播放形式
          AnimatorSet().apply {
              playSequentially(radiusAnim,moveAnim)
              addListener ( onEnd ={
                  longinStatus = STATUS_SUCCESS
                  //重绘
                  invalidate()
              } )
              start()
          }
        }

    fun addClickListener(callback:(LoginAnimationView)->Unit){
        setOnClickListener {
            callback(this)
        }
    }
}